package com.rfid.emergencywarehouse.activity;

import android.Manifest;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;

import com.rfid.base.base.BaseActivity;
import com.rfid.base.util.BaseUtil;
import com.rfid.base.util.PreferenceUtil;
import com.rfid.emergencywarehouse.App;
import com.rfid.emergencywarehouse.R;
import com.rfid.emergencywarehouse.net.NetApi;
import com.rfid.emergencywarehouse.presenter.LoginPresenter;
import com.rfid.emergencywarehouse.util.DialogUtil;
import com.rfid.emergencywarehouse.view.ILoginView;
import com.rfid.emergencywarehouse.event.LoginEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.Map;

public class LoginActivity extends BaseActivity<LoginPresenter>implements ILoginView {

    @Override
    public LoginPresenter getPresenter() {
        return new LoginPresenter();
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        DisplayMetrics metrics = getResources().getDisplayMetrics();
        PreferenceUtil.init(getApplicationContext());
        setContentView(R.layout.activity_login);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        findViewById(R.id.root).setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE|View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        System.out.println(metrics.densityDpi + ":" + metrics.density + ":" + metrics.widthPixels + ":" + metrics.heightPixels);
        EventBus.getDefault().register(this);

        askForStoragePermissions();

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
            App.getApp().init();
        } else {
            ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},100);
        }

        findViewById(R.id.textView).setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                String url = PreferenceUtil.getString("url",NetApi.DEFAULT_URL);
                EditText editText = new EditText(LoginActivity.this);
                editText.setText(url);
                editText.setWidth(960);
                AlertDialog dialog = new AlertDialog.Builder(LoginActivity.this,R.style.dialogStyle)
                        .setTitle("设置地址")
                        .setView(editText)
//                        .setNegativeButton("取消",null)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                String text = editText.getText().toString().trim();
                                if (TextUtils.isEmpty(text) || !text.startsWith("http")) {
                                    return;
                                }
                                PreferenceUtil.commitString("url",text);
                                NetApi.INSTANCE.init();
                            }
                        })
                        .show();
                return false;
            }
        });
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (ActivityCompat.checkSelfPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
            App.getApp().init();
        } else {
            Toast.makeText(this,"无读写文件权限",Toast.LENGTH_SHORT).show();
            finish();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void login(LoginEvent event) {
        mPresenter.login(event.getName(),event.getPwd());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onLoginSuccess(String name) {
        Map<String,String> kv = new HashMap<>();
        kv.put("name",name);
        BaseUtil.startActivity(this, HomeAcitvity.class,kv);
        finish();
    }

    @Override
    public void onLoginFail(String reason) {
        DialogUtil.showTipDialog(this,findViewById(R.id.root),reason);
    }
}
